package com.nami.store.biz.user.repository.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nami.store.biz.user.repository.UserRepository;
import com.nami.store.domain.UserDO;
import com.nami.store.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * im用户信息 服务实现类
 * </p>
 *
 * @author lbc
 * @since 2023-07-19
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class UserRepositoryImpl implements UserRepository {

    private final UserMapper userMapper;

    @Override
    public List<UserDO> getUserByIds(List<String> userIds) {
        List<UserDO> list = userMapper.selectList(new LambdaQueryWrapper<UserDO>().in(UserDO::getUserId, userIds));
        return list;
    }

    @Override
    public Map<String, UserDO> getUsers(List<String> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return new HashMap<>();
        }
        List<UserDO> users = this.getUserByIds(userIds);
        if (CollUtil.isEmpty(users)) {
            return new HashMap<>();
        }
        return users.stream().collect(Collectors.toMap(UserDO::getUserId, Function.identity()));
    }

    @Override
    public UserDO getUserByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }

        UserDO user = userMapper.selectOne(new LambdaQueryWrapper<UserDO>().eq(UserDO::getUserId, userId));
        if (null == user) {
            return null;
        }
        return user;
    }

    @Override
    public UserDO getUserById(String appKey, String userId) {
        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(userId)) {
            return null;
        }
        UserDO user = userMapper.selectOne(new LambdaQueryWrapper<UserDO>()
            .eq(UserDO::getAppKey, appKey)
            .eq(UserDO::getUserId, userId));
        return user;
    }

    @Override
    public UserDO getUserByAccount(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        UserDO user = userMapper.selectOne(new LambdaQueryWrapper<UserDO>().eq(UserDO::getAccount, account));
        return user;
    }

    @Override
    public boolean saveUser(UserDO user) {
        log.info("[user-save] save user:{}", JsonUtils.toJsonString(user));
        userMapper.insert(user);
        return Boolean.TRUE;
    }

    @Override
    public boolean batchSave(List<UserDO> users) {
        List<BatchResult> results = userMapper.insert(users);

        return Boolean.TRUE;
    }

    @Override
    public boolean updateUserById(UserDO user) {

        userMapper.updateById(user);
        return Boolean.TRUE;
    }

    @Override
    public UserDO getUserByMobile(String mobile) {
        return userMapper.selectOne(new LambdaQueryWrapper<UserDO>().eq(UserDO::getMobile, mobile));
    }

}
